<template>
  <el-dialog
    custom-class="comn_dialog m-dialog"
    :title="title"
    :visible.sync="dialogVisible"
    :width="width"
    :top="top"
    :close-on-click-modal="closeOnClickModal"
    :before-close="beforeClose"
    @close="$emit('close')"
    :append-to-body="appendToBody"
    :center="center"
    v-if="dialogVisible"
    v-dialogDrag="draggable"
  >
    <slot>
      <p>弹窗内容自定义</p>
    </slot>
    <div slot="footer" v-if="$slots.footer">
      <slot name="footer" />
    </div>
  </el-dialog>
</template>
<script>
export default {
  props: {
    title: {
      type: String,
      default: "标题"
    },
    visible: {
      type: Boolean,
      default() {
        return false;
      }
    },
    width: {
      type: String,
      default() {
        return "430px";
      }
    },
    top: {
      type: String,
      default() {
        return "15vh";
      }
    },
    closeOnClickModal: {
      type: Boolean,
      default: false
    },
    appendToBody: {
      type: Boolean,
      default: true
    },
    center: {
      type: Boolean,
      default: false
    },
    draggable: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    Cancel() {
      this.$emit("close");
    },
    beforeClose(done) {
      this.$emit("beforeClose");
      done();
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      }
    }
  },
  created() {},
  directives: {
    dialogDrag: {
      bind(el, binding) {
        // 自定义属性，判断是否可拖拽
        if (!binding.value) return;
        const dialogHeaderEl = el.querySelector(".el-dialog__header");
        const dragDom = el.querySelector(".el-dialog");
        dialogHeaderEl.style.cursor = "move";
        // 禁止拖拽时选中标题中文本内容
        dialogHeaderEl.style.userSelect = "none";
        // 获取原有属性
        const sty = (function() {
          if (document.body.currentStyle) {
            // 在 IE 下兼容写法
            return (dom, attr) => dom.currentStyle[attr];
          }
          return (dom, attr) => getComputedStyle(dom, null)[attr];
        })();

        dialogHeaderEl.onmousedown = e => {
          // 鼠标按下，计算当前元素距离可视区的距离
          const disX = e.clientX - dialogHeaderEl.offsetLeft;
          const disY = e.clientY - dialogHeaderEl.offsetTop;
          const screenWidth = document.body.clientWidth; // body当前宽度
          const screenHeight = document.documentElement.clientHeight; // 可见区域高度

          const dragDomWidth = dragDom.offsetWidth; // 对话框宽度
          const dragDomHeight = dragDom.offsetHeight; // 对话框高度

          const minDragDomLeft = dragDom.offsetLeft;
          const maxDragDomLeft =
            screenWidth - dragDom.offsetLeft - dragDomWidth;

          const minDragDomTop = dragDom.offsetTop;
          const maxDragDomTop =
            screenHeight - dragDom.offsetTop - dragDomHeight;

          // 获取到的值带px 正则匹配替换
          let styL = sty(dragDom, "left");
          if (styL === "auto") styL = "0px";
          let styT = sty(dragDom, "top");

          if (styL.includes("%")) {
            styL = +document.body.clientWidth * (+styL.replace(/%/g, "") / 100);
            styT =
              +document.body.clientHeight * (+styT.replace(/%/g, "") / 100);
          } else {
            styL = +styL.replace(/px/g, "");
            styT = +styT.replace(/px/g, "");
          }

          document.onmousemove = function(e) {
            // 通过事件委托，计算移动的距离
            let left = e.clientX - disX;
            let top = e.clientY - disY;

            // 边界处理
            if (-left > minDragDomLeft) {
              left = -minDragDomLeft;
            } else if (left > maxDragDomLeft) {
              left = maxDragDomLeft;
            }

            if (-top > minDragDomTop) {
              top = -minDragDomTop;
            } else if (top > maxDragDomTop) {
              top = maxDragDomTop;
            }

            // 移动当前元素
            dragDom.style.left = `${left + styL}px`;
            dragDom.style.top = `${top + styT}px`;
          };

          document.onmouseup = function() {
            document.onmousemove = null;
            document.onmouseup = null;
          };

          return false; // 防止文本选中
        };
      }
    }
  }
};
</script>
<style lang="scss">
.comn_dialog {
  .el-dialog__body {
    padding: 20px 20px 0px;
  }
}
</style>
